package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.zygh.policeasset.mapper.SectionMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.Section;
import com.zygh.policeasset.pojo.UploadFile;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SectionService {
    @Autowired
    SectionMapper sectionMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;

    public int add(Section section) {
        int sectionId = 0;
        try {
            this.sectionMapper.add(section);
            sectionId = section.getId().intValue();
            List<UploadFile> uploadFileList = section.getUploadFileList();
            for (int i = 0; i < uploadFileList.size(); i++) {
                uploadFileList.get(i).setProjectId(sectionId);
                uploadFileList.get(i).setProjectName("section");
                uploadFileList.get(i).setCreateDate(DateUtil.now());
                this.uploadFileMapper.add(uploadFileList.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sectionId;
    }

    public List<Section> findAll(Section section) {
        return this.sectionMapper.findAll(section);
    }

    public Section selectById(Integer id) {
        return this.sectionMapper.selectById(id);
    }

    public int update(Section section) {
        return this.sectionMapper.update(section);
    }

    public int delete(Integer id) {
        int res = 0;
        try {
            res = this.sectionMapper.delete(id);
            this.uploadFileMapper.delete(id.intValue(), "section");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public List<Map> getAddAssect(Section section) {
        return this.sectionMapper.getAddAssect(section);
    }
    public List<Section> selectProjectNo(String projectNo) {
        return this.sectionMapper.selectProjectNo(projectNo);
    }
}